package customer

import (
	"context"
	"fmt"
	"github.com/google/wire"
	"go.manyji.com/lib/log"
	"sieve_admin_server/app/api/util"
	"sieve_admin_server/app/contextx"
	"sieve_admin_server/app/schema"
	"sieve_admin_server/app/server"
	serviceCustomer "sieve_admin_server/app/service/customer"
)

var (
	_              util.API = (*UserRecordsAPI)(nil)
	UserRecordsSet          = wire.NewSet(wire.Struct(new(UserRecordsAPI), "*"))
)

type UserRecordsAPI struct {
	util.EmptyAPI
	UserRecordsSrv *serviceCustomer.UserRecordsSrv
}

func (a *UserRecordsAPI) GetApis() []*server.BatchModTree {
	return []*server.BatchModTree{
		{
			Obj:   a,
			Name:  "userRecords",
			Title: "用户操作记录", Perms: []*server.Perm{
				{Name: "query", Title: "搜索"},
				{Name: "export", Title: "导出"},
			},
			Order: 4,
		},
	}
}

func (a *UserRecordsAPI) API_query_list(ctx *server.Context) interface{} {
	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("id", true),
				contextx.NewVar("account"),
				contextx.NewVar("op_type_name"),
				contextx.NewVar("leader_id", true),
				contextx.NewVar("business_type_name"),
				contextx.NewVar("creator_name"),
				contextx.NewVar("creator_role_name"),
				contextx.NewVar("creator_role_alias"),
				contextx.NewVar("creator_name_like"),
				contextx.NewVar("creator_role_name_like"),
				contextx.NewVar("creator_role_alias_like"),
				contextx.NewVar("role_id", true),
				contextx.NewVar("start", true),
				contextx.NewVar("end", true),
				contextx.NewVar("status", true),
				contextx.NewVar("sieve_source_name"),
			},
		)

		p = schema.PaginationParam{
			PageIndex: util.ToInt(ctx.Args.Get("page_index")),
			PageSize:  util.ToInt(ctx.Args.Get("page_size")),
		}
	)

	return a.UserRecordsSrv.Query(context.Background(), uint64(ctx.Uid), reqArgs, p)
}

func (a *UserRecordsAPI) API_export_list(ctx *server.Context) interface{} {

	var (
		reqArgs = contextx.GetRequestArgs(ctx,
			[]contextx.Var{
				contextx.NewVar("ids"),
			},
		)
	)

	return a.UserRecordsSrv.Export(context.Background(), reqArgs, func(fileName string) {

		log.Info(fmt.Sprintf("export file %v", fileName))
		//http.ServeFile(ctx.Http.ResponseWriter, ctx.Http.Request, fileName)
	})
}
